package _43_左旋转字符串;

/**
 * 循环左移动字符串
 */
public class Solution {
    public String LeftRotateString(String str, int n) {
        if (str.length() == 0) return "";
        char[] c = str.toCharArray();

        reverse(c, 0, c.length - 1);
        reverse(c, c.length  - n, c.length - 1);
        reverse(c, 0, c.length - n - 1);
        return new String(c);
    }

    void reverse(char[] c, int start, int end) {
        while (start < end) {
            char tmp = c[start];
            c[start] = c[end];
            c[end] = tmp;
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.LeftRotateString("abcXYZdef",3));
    }
}
